package aes.gui;

import java.awt.CardLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import aes.general.TeacherControl;

/**
 * This panel allows the teacher to set activation code for
 * any of her exams, thus making it possible for students 
 * to make them after receiving the code orally.  
 */
public class TeacherActivationPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JPanel jp;
	private JButton bsave;
	private JButton bcancel;
	private JButton b3;
	private JLabel title;
	private JLabel l2;
	private JTextField t1;
	private JComboBox cb;
	private JTextArea txt;
	
	private ArrayList<String> Active = new ArrayList<String>();
	private ArrayList<String> ExamToActive = new ArrayList<String>();
	private boolean flag = false;
	/**
	 * Create the panel.
	 */
	public TeacherActivationPanel(JPanel contentPane) {
		this.jp = contentPane;
		initialize();
	}
	
	/**
	 * Initialize.
	 */
	private void initialize() {
		setLayout(null);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(10, 43, 620, 2);
		add(separator);
		
		title = new JLabel("Activate Exam");
		title.setFont(new Font("Tempus Sans ITC", Font.BOLD, 21));
		title.setBounds(235, 11, 169, 29);
		add(title);
		
		bsave = new JButton("Save");
		bsave.setBounds(218, 398, 91, 23);
		add(bsave);
		bsave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if ((String) cb.getSelectedItem() == "-------")
					JOptionPane.showMessageDialog(null,"Error,No Exam Selected!","Error",
							JOptionPane.ERROR_MESSAGE);
				else if(t1.getText().trim().length() != 4)
					JOptionPane.showMessageDialog(null,"Error,Activation Code Must Be 4 Digits!","Error",
							JOptionPane.ERROR_MESSAGE);
				else
				{
					ExamToActive.add((String)cb.getSelectedItem());
					ExamToActive.add(t1.getText());
											
					TeacherControl.SetActivationCode(ExamToActive);
					try {
						Thread.sleep(600);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					if(TeacherControl.ActivationAnswer() == false){
						JOptionPane.showMessageDialog(null,"Activation Code Is In Use," +
								"Please Try Again","Error",
								JOptionPane.ERROR_MESSAGE);
						ExamToActive.clear();
						//ExamToActive.add((String)cb.getSelectedItem());
						t1.setText(null);
					}
					else{
					JOptionPane.showMessageDialog(null,"Activation Code Has Been Set!","Ok",
							JOptionPane.INFORMATION_MESSAGE);
					
					ExamToActive.clear();
					cb.removeAllItems();
					cb.addItem("-------");
					t1.setText(null);
					flag = false;
					
					CardLayout cl = (CardLayout) (jp.getLayout());
					cl.show(jp, "mainPanel");}
				}	
			}
		});
		
		bcancel = new JButton("Cancel");
		bcancel.setBounds(333, 398, 91, 23);
		add(bcancel);
		bcancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				cb.removeAllItems();
				cb.addItem("-------");
				t1.setText(null);
				flag = false;
				CardLayout cl = (CardLayout) (jp.getLayout());
				cl.show(jp, "mainPanel");
			}
		});
		
		l2 = new JLabel("Choose Exam To activate:");
		l2.setBounds(88, 110, 164, 14);
		add(l2);
		
		cb = new JComboBox();
		cb.setBounds(247, 107, 85, 20);
		add(cb);
		cb.addItem("-------");
	
		JLabel l3 = new JLabel("Enter Activation Code:");
		l3.setBounds(88, 182, 144, 14);
		add(l3);
		
		t1 = new JTextField();
		t1.setBounds(246, 179, 86, 20);
		add(t1);
		t1.setColumns(10);
		
		txt = new JTextArea();
		txt.setEditable(false);
		txt.setFont(new Font("Courier New", Font.BOLD, 14));
		txt.setRows(3);
		txt.setText("The activation code must be \r\n4 digits long and may consist \r\nof letters and numbers only.");
		txt.setBounds(363, 179, 256, 70);
		add(txt);
		
		b3 = new JButton("Get Exams");
		b3.setBounds(363, 106, 107, 23);
		add(b3);
		b3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(flag == false){
				TeacherControl.RequestActiveExams(Active);
				try {
					Thread.sleep(600);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Active = TeacherControl.getActiveExams(); 
				for (int i=0;i<TeacherControl.getActiveExams().size();i++)
					cb.addItem(TeacherControl.getActiveExams().get(i));
				flag = true;
			}
			
			}
		});
	}
}
